package org.jeecg.modules.simulator.rooter;

import lombok.Getter;
import lombok.Setter;
import org.jeecg.modules.simulator.model.MetroTrain;
import org.jeecg.modules.simulator.model.line.Line;
import org.jeecg.modules.simulator.model.line.park.Parker;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.train.action.TrainActionRecorders;
import org.jeecg.modules.simulator.vo.LineCurInfoVo;
import org.jeecg.modules.simulator.recorder.ParkCurInfoVo;
import org.jeecg.modules.simulator.vo.TrainCurInfoVo;
import org.jeecg.modules.simulator.vo.TrainDyInfo;
import org.jeecg.modules.simulator.vo.TrainPosition;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2024-11-01 13:57
 **/
public class LineInfoPublisher {
    @Setter
    String sceneCode;
    @Setter
    @Getter
    Line line;
    @Getter
    @Setter
    TrainManager trainManager = new TrainManager();
    @Setter
    @Getter
    Map<String, Parker> parkerMap = new HashMap<>();//当前线路的场段

    @Setter
    @Getter
    private Map<String, Station> stationInThisLineMap = new HashMap<>();





    public static final String COMMON_DIRECTORY = "D:/aaaaaa/stationjsonfile/";
    public ParkCurInfoVo linePublishParkInfo() {

        return getParkNum();


    }





    //统计当前线路内在线列车数
    public int getOnlineTrainCount() {
        return trainManager.getTrainMap().size();
    }

    /**
     * @description: 获取线路当前车站信息
     * @author: renzhg
     * @date: 2024/11/1 14:22
     **/
    public LineCurInfoVo linePublishInfo(String now) {
        LineCurInfoVo lineCurInfoVo = new LineCurInfoVo();
        lineCurInfoVo.setSceneCode(sceneCode);
        lineCurInfoVo.setLineId(line.getLineId());
        lineCurInfoVo.setLineName(line.getLineName());
        lineCurInfoVo.setTimePt(now);
        Map<String, MetroTrain> trainMapOnLine = trainManager.getTrainMap();
        lineCurInfoVo.setTrainOnLineNum(trainMapOnLine.size());
        lineCurInfoVo.setLineTrainSchedulesName("");
        Map<String, TrainDyInfo> allTrainDyInfo = trainManager.getTrainStateStatisticser().getAllTrainDyInfo(now);


        double lsFullLoad=0.0;

        int curTrainPersonNum=0;
        for(Map.Entry<String, TrainDyInfo> trainDyInfoEntry : allTrainDyInfo.entrySet()) {
            if(trainMapOnLine.containsKey(trainDyInfoEntry.getKey())){
                lsFullLoad=lsFullLoad+Double.parseDouble(trainDyInfoEntry.getValue().getCurFullLoad());
                curTrainPersonNum=curTrainPersonNum+trainDyInfoEntry.getValue().getCurPersonNumbers();

            }
        }
        lineCurInfoVo.setTrainFullLoadLine(lsFullLoad);
double lsStationFullLoad=0.0;
int inputLsInstantPersonNum=0;
int outputLsInstantPersonNum=0;


            for(Map.Entry<String,Station> mapEntry: stationInThisLineMap.entrySet()){
                lineCurInfoVo.getStationInstantCurOutputPersonNumMap().put(mapEntry.getKey(), mapEntry.getValue().getStationInstantInfo().getStationInstantOutPersonNum());
                lineCurInfoVo.getStationInstantCurWaitPersonNumMap().put(mapEntry.getKey(), mapEntry.getValue().getStationInstantInfo().getStationInstantWaitPersonNum());
                lineCurInfoVo.getStationInstantWalkingPersonNumMap().put(mapEntry.getKey(), mapEntry.getValue().getStationInstantInfo().getStationInstantWaitPersonNum());
                lineCurInfoVo.getStationInstantCurPersonNumMap().put(mapEntry.getKey(), mapEntry.getValue().getStationInstantInfo().getStationInstantInStationPersonNum());

                lsStationFullLoad=lsStationFullLoad+mapEntry.getValue().getFullLoad();
                Integer instantInSationtNum=0;
                if(mapEntry.getValue().getStationInstantInfo().getStationInstantInStationPersonNumMap().get(now)!=null){
                    System.out.println(mapEntry.getValue().getStationInstantInfo());
                    System.out.println(mapEntry.getValue().getStationInstantInfo().getStationInstantInStationPersonNumMap());
                    instantInSationtNum=mapEntry.getValue().getStationInstantInfo().getStationInstantInStationPersonNumMap().get(now);
                }

                inputLsInstantPersonNum=inputLsInstantPersonNum+instantInSationtNum;


                Integer  ItStationInstantOutPersonNum=0;
                if(mapEntry.getValue().getStationInstantInfo().getStationInstantOutPersonNumMap().get(now)!=null){
                    ItStationInstantOutPersonNum=mapEntry.getValue().getStationInstantInfo().getStationInstantOutPersonNumMap().get(now);
                }


                outputLsInstantPersonNum=outputLsInstantPersonNum+ItStationInstantOutPersonNum;
            }


//            lineCurInfoVo.getStationInstantCurWantInputPersonNumMap().put(k, v.getStationInstantInfo().getStationTotalInStationPersonNum());



        lineCurInfoVo.setInLineInstantCurPersonNum(curTrainPersonNum+inputLsInstantPersonNum);


        lineCurInfoVo.setInLineTotalCurPersonNum(lineCurInfoVo.getInLineInstantCurPersonNum()+lineCurInfoVo.getInLineTotalCurPersonNum());

        lineCurInfoVo.setInputTotalLineNum(lineCurInfoVo.getInputTotalLineNum()+inputLsInstantPersonNum);
        lineCurInfoVo.setOutputTotalLineNum(lineCurInfoVo.getOutputTotalLineNum()+outputLsInstantPersonNum);

//        });
        lineCurInfoVo.setStationFullLoadLine(lsStationFullLoad);
        return lineCurInfoVo;

    }

    /**
     * @description: 获取当前车辆行驶信息
     * @author: renzhg
     * @date: 2024/11/1 14:22
     **/
    public TrainCurInfoVo trainCurInfoPublish(String now) {
        TrainCurInfoVo trainCurInfoVo = new TrainCurInfoVo();
        trainCurInfoVo.setSceneCode(sceneCode);
        Map<String, TrainPosition> allCurTrainPosition = this.trainManager.getAllTrainPosition(now);
        Map<String, TrainDyInfo> allTrainDyInfo = this.trainManager.getTrainStateStatisticser().getAllTrainDyInfo(now);
        Map<String,Integer> allTrainPersons= this.trainManager.getTrainStateStatisticser().getAllTrainPersons(now);

        trainCurInfoVo.setAllCurTrainPosition(allCurTrainPosition);
        trainCurInfoVo.setAllCurTrainDyInfo(allTrainDyInfo);
        trainCurInfoVo.setTrainCurPersonNum(allTrainPersons);
        return trainCurInfoVo;
    }

    public void trainRecorderPublish(String now) {
        //TODO
        this.trainManager.getTrainStateStatisticser().generateTrainRecorder(now);
    }



    public int getOfflineTrainCount() {
        int onlineTrainCount = 0;
        for (Map.Entry<String, Parker> entry : parkerMap.entrySet()) {
            Parker value = entry.getValue();
            onlineTrainCount=onlineTrainCount+value.getNotOnlineTrainMap().size();
        }
        return onlineTrainCount;
    }
    //统计当前线路总人数

    public int getLinePeopleNum() {
        int linePeopleNum = 0;
        for (Map.Entry<String, Station> entry : stationInThisLineMap.entrySet()) {
            Station value = entry.getValue();
            linePeopleNum = linePeopleNum + value.getAllPersonMap().size();
        }
        return linePeopleNum;
    }
    //统计当前站内进站人数

    //统计当前车内人数
    public int getLineTrainPeopleNum() {
        int trainPeopleNum = 0;
        for (Map.Entry<String, MetroTrain> entry : trainManager.getTrainMap().entrySet()) {
            MetroTrain value = entry.getValue();
            trainPeopleNum = trainPeopleNum + value.getPersonMap().size();
        }
        return trainPeopleNum;
    }




    //统计分线各个停车场的要素
    public ParkCurInfoVo getParkNum() {
        ParkCurInfoVo parkCurInfoVo = new ParkCurInfoVo();
        //当前没有上线的列车数
        Map<String, Integer> parkCurNotOnlineTrainNum = new HashMap<>();
        //当前车辆动作记录数据
        Map<String, TrainActionRecorders> trainCurTrainActionRecordersMap = new HashMap<>();
        ;
        parkerMap.forEach((k, v) -> {
            parkCurNotOnlineTrainNum.put(v.getName(), v.getNotOnlineTrainMap().size());
            trainCurTrainActionRecordersMap.putAll(v.getTrainActionRecordersMap());
        });
        return parkCurInfoVo;
    }


//    /**
//     * @description: 记录各站内人数
//     * @author: renzhg
//     * @date: 2024/11/1 14:35
//     **/
//    public Map<String, Integer> getStationPeopleNum() {
//        Map<String, Integer> stationPeopleNum = new HashMap<>();
//        stationInThisLineMap.forEach((k, v) -> {
//            stationPeopleNum.put(k + "-" + line.getLineId(), );
//
//
//
//
//        });
//        return stationPeopleNum;
//    }

    /**
     * @description: 记录走行人数
     * @author: renzhg
     * @date: 2024/11/1 14:33
     **/
    public Map<String, Integer> getStationWalkingPeopleNum() {
        Map<String, Integer> stationPeopleNum = new HashMap<>();
        stationInThisLineMap.forEach((k, v) -> {
            stationPeopleNum.put(k + "-" + line.getLineId(), v.getWalkingPersonMap().size());

        });
        return stationPeopleNum;
    }


    /**
     * @description: 记录等车人数
     * @author: renzhg
     * @date: 2024/11/1 14:26
     **/
    public Map<String, Integer> getStationWaitingPeopleNum() {
        Map<String, Integer> stationPeopleNum = new HashMap<>();
        stationInThisLineMap.forEach((k, v) -> {
            stationPeopleNum.put(k + "-" + line.getLineId(), v.getWaitingPersonMap().size());

        });
        return stationPeopleNum;
    }

    /**
     * @description: 记录各个站进站的人
     * @author: renzhg
     * @date: 2024/11/1 14:25
     **/
    public Map<String, Integer> getStationOutsidePeopleNum() {
        Map<String, Integer> stationPeopleNum = new HashMap<>();
        stationInThisLineMap.forEach((k, v) -> {
            stationPeopleNum.put(k + "-" + line.getLineId(), v.getOutsidePersonMap().size());
        });
        return stationPeopleNum;
    }

    /**
     * @description: 记录各个出站的人
     * @author: renzhg
     * @date: 2024/11/1 14:26
     **/
    public Map<String, Integer> getStationOutPeopleNum() {
        Map<String, Integer> stationPeopleNum = new HashMap<>();
        stationInThisLineMap.forEach((k, v) -> {
            stationPeopleNum.put(k + "-" + line.getLineId(), v.getOutStationPersonMap().size());

        });
        return stationPeopleNum;
    }
}
